package com.net.TCP04.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 创建服务器：发送数据+接收数据
 * *写出数据：输出流
 * 读取数据：输入流
 * @author jiangbang
 * @date 2016/7/26
 */
public class Server {
    public static void main(String[] args) throws IOException {
        //1、创建服务器   指定端口ServerSocket（int port）
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket client = serverSocket.accept();
        //输入流 输出流
        DataInputStream dis = new DataInputStream(client.getInputStream());
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());


        String msg = dis.readUTF();
        //
        dos.writeUTF("服务器--》"+msg);
        dos.flush();
    }

    //原始版服务器
    public static void demoServer(String[] args) throws IOException {
        //1、创建服务器   指定端口ServerSocket（int port）
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket client = serverSocket.accept();
        //输入流
        DataInputStream dis = new DataInputStream(client.getInputStream());
        String msg = dis.readUTF();
        //输出流
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        dos.writeUTF("服务器--》"+msg);
        dos.flush();
    }
}
